package com.dhj.methodsTest;

import com.dhj.streamTest.entity.Author;

import java.util.List;
import java.util.stream.Stream;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo7 {

    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        Long startTime = System.currentTimeMillis();
        Integer sum = stream.filter(num -> num % 2 == 0)
                .reduce((res, ele) -> res += ele)
                .get();

        System.out.println("time=" + (System.currentTimeMillis() - startTime) + ", sum=" + sum);


        System.out.println("----------------------------------------------------");

        stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        startTime = System.currentTimeMillis();
        Integer sum2 = stream.parallel().filter(num -> num % 2 == 0)
                .reduce((res, ele) -> res += ele)
                .get();

        System.out.println("time=" + (System.currentTimeMillis() - startTime) + ", sum=" + sum2);


        System.out.println("----------------------------------------------------");

        stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        startTime = System.currentTimeMillis();
        Integer sum3 = stream.parallel().filter(num -> num % 2 == 0)
                .peek(num -> System.out.println(Thread.currentThread().getName() + " --- " + num))
                .reduce((res, ele) -> res += ele)
                .get();

        System.out.println("time=" + (System.currentTimeMillis() - startTime) + ", sum=" + sum3);


        System.out.println("----------------------------------------------------");

        List<Author> authors = getAuthors();
        authors.parallelStream().filter(author -> author.getAge()>50)
                .forEach(author -> System.out.println(author.getName()+"---"+author.getAge()));



    }

}
